package org.mspring.mlog.search;

import cn.mspring.nbee.search.DefaultSearchService;
import com.alibaba.fastjson.JSON;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.TermQuery;
import org.mspring.mlog.cache.ArticleCacheService;
import org.mspring.mlog.dao.ArticleEntityDao;
import org.mspring.mlog.entity.ArticleEntity;
import org.mspring.nbee.common.PageResult;
import org.mspring.nbee.common.Pager;
import org.mspring.nbee.common.ThreadPool;
import org.mspring.nbee.db.dao.impl.EntitySequenceHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Gao Youbo
 * @since 2015-10-21 22:47
 */
@Service
public class ArticleSearchService extends DefaultSearchService<ArticleSearchEntity> {

    private static final Logger LOG = LoggerFactory.getLogger(ArticleSearchService.class);

    private ArticleEntityDao articleEntityDao;
    private ArticleCacheService articleCacheService;
    private ThreadPool threadPool;

    public ArticleSearchService() {
        super("/data/mlog/lucene");
    }

    public void indexAll() {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                articleEntityDao.listById(new EntitySequenceHandler<ArticleEntity>() {
                    @Override
                    public void handleEntity(ArticleEntity entity) {
                        addIndex(entity);
                    }
                });
            }
        });
    }

    public static void main(String[] args) {
        ArticleSearchService articleSearchService = new ArticleSearchService();
        //MultiPhraseQuery query = new MultiPhraseQuery();
        //query.add(new Term("content", "java"));
        //query.add(new Term("title", "java"));

        //PhraseQuery query = new PhraseQuery.Builder()
        //        .add(new Term("content", "java"))
        //        .add(new Term("title", "java")).build();

        BooleanQuery booleanQuery = new BooleanQuery.Builder()
                .add(new TermQuery(new Term("content", "java")), BooleanClause.Occur.SHOULD)
                .add(new TermQuery(new Term("title", "java")), BooleanClause.Occur.SHOULD)
                .build();

        Pager pager = new Pager();
        PageResult<ArticleSearchEntity> result = articleSearchService.search(booleanQuery, pager);
        System.out.println(JSON.toJSONString(result, true));
    }


    public void addIndex(long id) {
        ArticleEntity article = articleCacheService.getArticle(id);
        addIndex(article);
    }

    public void addIndex(ArticleEntity article) {
        ArticleSearchEntity entity = ArticleSearchEntity.fromEntity(article);
        addIndex(String.valueOf(entity.getId()), entity);
        LOG.info("构建文章索引, id={}", article.getId());
    }

    @Autowired
    public void setArticleEntityDao(ArticleEntityDao articleEntityDao) {
        this.articleEntityDao = articleEntityDao;
    }

    @Autowired
    public void setArticleCacheService(ArticleCacheService articleCacheService) {
        this.articleCacheService = articleCacheService;
    }

    @Autowired
    public void setThreadPool(ThreadPool threadPool) {
        this.threadPool = threadPool;
    }
}
